package net.expectx.pay.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 控制器基类
 *
 * @author jianhun
 * @date 2018/5/21
 */
public abstract class BaseController {

	private final static Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

	/**
	 * 统一异常处理
	 * @param request
	 * @param response
	 * @param exception
	 */
	@ExceptionHandler
	public String exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
		LOGGER.error("统一异常处理：", exception);
		request.setAttribute("ex", exception);
		String xRequestedWith = "X-Requested-With";
		String xMLHttpRequest = "XMLHttpRequest";
		if (null != request.getHeader(xRequestedWith)
				&& xMLHttpRequest.equalsIgnoreCase(request.getHeader(xRequestedWith))) {
			request.setAttribute("requestHeader", "ajax");
		}
		/*// shiro没有权限异常
		if (exception instanceof UnauthorizedException) {
			return "/403.jsp";
		}
		// shiro会话已过期异常
		if (exception instanceof InvalidSessionException) {
			return "/error.jsp";
		}*/
		return "/error.jsp";
	}

	/**
	 * 返回jsp视图
	 * @param path
	 * @return
	 */
	public static String jsp(String path) {
		return path.concat(".jsp");
	}

	/**
	 * 返回thymeleaf视图
	 * @param path
	 * @return
	 */
	/*public static String thymeleaf(String path) {
		String folder = PropertiesFileUtil.getInstance().get(BaseConstants.APP_NAME);
		return "/".concat(folder).concat(path).concat(".html");
	}*/

}
